package com.rowenci.iot_agriculture.service.impl;

import com.rowenci.iot_agriculture.bean.admin.Loginlog;
import com.rowenci.iot_agriculture.mapper.LoginlogDao;
import com.rowenci.iot_agriculture.service.api.LoginlogService;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import java.util.Optional;

/**
 * <p>
 *
 * </p>
 *
 * @author rowenci
 * @date 2019/4/21 18:41
 */
@Service
public class LoginlogServiceImpl implements LoginlogService {
    
    @Resource
    private LoginlogDao loginlogDao;

    @Override
    public List<Loginlog> findAll() {
        return loginlogDao.findAll();
    }

    @Override
    public void addLog(Loginlog loginlog) {
        if (loginlogDao == null){
            loginlogDao = new LoginlogDao() {
                @Override
                public List<Loginlog> findAll() {
                    return null;
                }

                @Override
                public List<Loginlog> findAll(Sort sort) {
                    return null;
                }

                @Override
                public List<Loginlog> findAllById(Iterable<Integer> iterable) {
                    return null;
                }

                @Override
                public <S extends Loginlog> List<S> saveAll(Iterable<S> iterable) {
                    return null;
                }

                @Override
                public void flush() {

                }

                @Override
                public <S extends Loginlog> S saveAndFlush(S s) {
                    return null;
                }

                @Override
                public void deleteInBatch(Iterable<Loginlog> iterable) {

                }

                @Override
                public void deleteAllInBatch() {

                }

                @Override
                public Loginlog getOne(Integer integer) {
                    return null;
                }

                @Override
                public <S extends Loginlog> List<S> findAll(Example<S> example) {
                    return null;
                }

                @Override
                public <S extends Loginlog> List<S> findAll(Example<S> example, Sort sort) {
                    return null;
                }

                @Override
                public Page<Loginlog> findAll(Pageable pageable) {
                    return null;
                }

                @Override
                public <S extends Loginlog> S save(S s) {
                    String driver = "com.mysql.cj.jdbc.Driver";
                    String url = "jdbc:mysql://cdb-qhgx4ekn.gz.tencentcdb.com:10000/iot_agriculture?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=true";
                    String username = "root";
                    String password = "bb3960519";
                    Connection conn = null;
                    try {
                        Class.forName(driver); //classLoader,加载对应驱动
                        conn = (Connection) DriverManager.getConnection(url, username, password);
                    } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }

                    int i = 0;
                    String sql = "insert into loginlog (rfid_id, worker_id, login_time, greenhouse_id) values(?,?,?,?)";
                    PreparedStatement pstmt;
                    try {
                        pstmt = (PreparedStatement) conn.prepareStatement(sql);
                        pstmt.setInt(1, s.getRfid_id());
                        pstmt.setInt(2, s.getWorker_id());
                        pstmt.setString(3, s.getTime());
                        pstmt.setInt(4, s.getGreenhouse_id());
                        i = pstmt.executeUpdate();
                        System.out.println("resutl: " + i);
                        pstmt.close();
                        conn.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                    return null;
                }

                @Override
                public Optional<Loginlog> findById(Integer integer) {
                    return Optional.empty();
                }

                @Override
                public boolean existsById(Integer integer) {
                    return false;
                }

                @Override
                public long count() {
                    return 0;
                }

                @Override
                public void deleteById(Integer integer) {

                }

                @Override
                public void delete(Loginlog loginlog) {

                }

                @Override
                public void deleteAll(Iterable<? extends Loginlog> iterable) {

                }

                @Override
                public void deleteAll() {

                }

                @Override
                public <S extends Loginlog> Optional<S> findOne(Example<S> example) {
                    return Optional.empty();
                }

                @Override
                public <S extends Loginlog> Page<S> findAll(Example<S> example, Pageable pageable) {
                    return null;
                }

                @Override
                public <S extends Loginlog> long count(Example<S> example) {
                    return 0;
                }

                @Override
                public <S extends Loginlog> boolean exists(Example<S> example) {
                    return false;
                }
            };
        }
        loginlogDao.save(loginlog);
    }
}
